"""
WorldQuant 101 因子策略 - Z-score 归一化版本
使用增量更新和 Z-score 标准化
"""
import numpy as np
import pandas as pd
from .base_strategy import BaseStrategy


class WQ101ZscoreStrategy(BaseStrategy):
    strategy_name = "wq101_zscore"
    
    def __init__(self, api, symbol_info, market_period):
        super().__init__(api, symbol_info, market_period)
        for symbol in symbol_info.keys():
            # 存储历史因子原始值序列（用于增量更新和 Z-score 计算）
            self.all_info[symbol]["alpha001_series"] = []
            self.all_info[symbol]["alpha002_series"] = []
            self.all_info[symbol]["alpha003_series"] = []
            self.all_info[symbol]["alpha006_series"] = []
            self.all_info[symbol]["alpha011_series"] = []
            self.all_info[symbol]["alpha020_series"] = []
            self.all_info[symbol]["alpha023_series"] = []
            self.all_info[symbol]["alpha027_series"] = []
            self.all_info[symbol]["alpha030_series"] = []
            self.all_info[symbol]["alpha032_series"] = []
            self.all_info[symbol]["alpha041_series"] = []
            self.all_info[symbol]["alpha045_series"] = []
            self.all_info[symbol]["alpha053_series"] = []
            self.all_info[symbol]["alpha057_series"] = []
            self.all_info[symbol]["alpha065_series"] = []
            self.all_info[symbol]["alpha081_series"] = []
            self.all_info[symbol]["alpha084_series"] = []
            self.all_info[symbol]["alpha093_series"] = []
            self.all_info[symbol]["alpha094_series"] = []
            self.all_info[symbol]["alpha101_series"] = []
            
            # Z-score 计算窗口（拉长到 200-600）
            self.all_info[symbol]["zscore_window"] = symbol_info[symbol].get("zscore_window", 300)
            self.all_info[symbol]["max_series_length"] = symbol_info[symbol].get("max_series_length", 800)
    
    def calculate_signal(self, symbol, current_pos, kline):
        """计算综合信号 - 增量更新 + Z-score 版本"""
        # 更新所有因子的原始值序列
        self._update_alpha_series(symbol, kline)
        
        # 计算 Z-score 归一化后的因子值
        alpha001 = self._calculate_zscore(symbol, "alpha001_series")
        alpha002 = self._calculate_zscore(symbol, "alpha002_series")
        alpha003 = self._calculate_zscore(symbol, "alpha003_series")
        alpha006 = self._calculate_zscore(symbol, "alpha006_series")
        alpha011 = self._calculate_zscore(symbol, "alpha011_series")
        alpha020 = self._calculate_zscore(symbol, "alpha020_series")
        alpha023 = self._calculate_zscore(symbol, "alpha023_series")
        alpha027 = self._calculate_zscore(symbol, "alpha027_series")
        alpha030 = self._calculate_zscore(symbol, "alpha030_series")
        alpha032 = self._calculate_zscore(symbol, "alpha032_series")
        alpha041 = self._calculate_zscore(symbol, "alpha041_series")
        alpha045 = self._calculate_zscore(symbol, "alpha045_series")
        alpha053 = self._calculate_zscore(symbol, "alpha053_series")
        alpha057 = self._calculate_zscore(symbol, "alpha057_series")
        alpha065 = self._calculate_zscore(symbol, "alpha065_series")
        alpha081 = self._calculate_zscore(symbol, "alpha081_series")
        alpha084 = self._calculate_zscore(symbol, "alpha084_series")
        alpha093 = self._calculate_zscore(symbol, "alpha093_series")
        alpha094 = self._calculate_zscore(symbol, "alpha094_series")
        alpha101 = self._calculate_zscore(symbol, "alpha101_series")
        
        # 综合所有因子信号（等权重）
        alphas = [
            alpha001, alpha002, alpha003, alpha006, alpha011, alpha020,
            alpha023, alpha027, alpha030, alpha032, alpha041, alpha045,
            alpha053, alpha057, alpha065, alpha081, alpha084, alpha093,
            alpha094, alpha101
        ]
        valid_alphas = [a for a in alphas if a is not None]
        
        if not valid_alphas:
            return 0, 0.0
        
        combined_score = np.mean(valid_alphas)
        
        # 获取 tanh 缩放系数（控制信号敏感度）
        tanh_scale = self.all_info[symbol].get("tanh_scale", 1.0)
        
        # 使用 tanh 将 combined_score 映射到 [-1, 1] 区间作为目标仓位
        # tanh_scale 越大，信号越敏感（更快达到满仓）
        # tanh_scale 越小，信号越平滑（需要更强的信号才满仓）
        target_position = np.tanh(combined_score * tanh_scale)
        target_position = combined_score
        
        # DEBUG: 打印前几次的得分
        if len(self.all_info[symbol]["alpha001_series"]) <= 50:
            print(f"Bar {len(self.all_info[symbol]['alpha001_series'])}: combined={combined_score:.3f}, target_pos={target_position:.3f}")
        
        # 返回目标仓位（-1到1之间的连续值）
        return target_position, combined_score
    
    def _prepare_cache(self, symbol, kline):
        """预计算常用数据"""
        cache = {}
        max_lookback = max(
            self.all_info[symbol].get("alpha001_period", 5),
            self.all_info[symbol].get("alpha002_period", 10),
            self.all_info[symbol].get("alpha003_period", 10),
            self.all_info[symbol].get("alpha006_period", 10),
            self.all_info[symbol].get("alpha011_period", 5),
            self.all_info[symbol].get("alpha020_period", 5)
        )
        
        if len(kline.close) >= max_lookback + 1:
            cache['close'] = kline.close.values[-(max_lookback+1):]
            cache['open'] = kline.open.values[-(max_lookback+1):]
            cache['high'] = kline.high.values[-(max_lookback+1):]
            cache['low'] = kline.low.values[-(max_lookback+1):]
            cache['volume'] = kline.volume.values[-(max_lookback+1):]
        
        return cache
    
    def _update_alpha_series(self, symbol, kline):
        """批量初始化 + 增量更新所有因子序列（统一循环）"""
        close_prices = kline.close.values
        open_prices = kline.open.values
        high_prices = kline.high.values
        low_prices = kline.low.values
        volume_prices = kline.volume.values
        
        current_len = len(close_prices)
        max_length = self.all_info[symbol]["max_series_length"]
        
        # 获取所有序列和参数
        series_dict = {
            'alpha001': self.all_info[symbol]["alpha001_series"],
            'alpha002': self.all_info[symbol]["alpha002_series"],
            'alpha003': self.all_info[symbol]["alpha003_series"],
            'alpha006': self.all_info[symbol]["alpha006_series"],
            'alpha011': self.all_info[symbol]["alpha011_series"],
            'alpha020': self.all_info[symbol]["alpha020_series"],
            'alpha023': self.all_info[symbol]["alpha023_series"],
            'alpha027': self.all_info[symbol]["alpha027_series"],
            'alpha030': self.all_info[symbol]["alpha030_series"],
            'alpha032': self.all_info[symbol]["alpha032_series"],
            'alpha041': self.all_info[symbol]["alpha041_series"],
            'alpha045': self.all_info[symbol]["alpha045_series"],
            'alpha053': self.all_info[symbol]["alpha053_series"],
            'alpha057': self.all_info[symbol]["alpha057_series"],
            'alpha065': self.all_info[symbol]["alpha065_series"],
            'alpha081': self.all_info[symbol]["alpha081_series"],
            'alpha084': self.all_info[symbol]["alpha084_series"],
            'alpha093': self.all_info[symbol]["alpha093_series"],
            'alpha094': self.all_info[symbol]["alpha094_series"],
            'alpha101': self.all_info[symbol]["alpha101_series"],
        }
        
        lookback_dict = {
            'alpha001': self.all_info[symbol].get("alpha001_period", 5),
            'alpha002': self.all_info[symbol].get("alpha002_period", 10),
            'alpha003': self.all_info[symbol].get("alpha003_period", 10),
            'alpha006': self.all_info[symbol].get("alpha006_period", 10),
            'alpha011': self.all_info[symbol].get("alpha011_period", 5),
            'alpha020': self.all_info[symbol].get("alpha020_period", 5),
            'alpha023': self.all_info[symbol].get("alpha023_period", 20),
            'alpha027': self.all_info[symbol].get("alpha027_period", 10),
            'alpha030_month': self.all_info[symbol].get("alpha030_month", 20),
            'alpha030_year': self.all_info[symbol].get("alpha030_year", 240),
            'alpha032': self.all_info[symbol].get("alpha032_period", 20),
            'alpha041': self.all_info[symbol].get("alpha041_period", 10),
            'alpha045': self.all_info[symbol].get("alpha045_period", 10),
            'alpha053': 2,  # 只需要2根K线
            'alpha057': self.all_info[symbol].get("alpha057_period", 10),
            'alpha065': self.all_info[symbol].get("alpha065_period", 5),
            'alpha081': self.all_info[symbol].get("alpha081_period", 20),
            'alpha084': self.all_info[symbol].get("alpha084_period", 20),
            'alpha093': self.all_info[symbol].get("alpha093_period", 20),
            'alpha094': self.all_info[symbol].get("alpha094_period", 5),
            'alpha101': 1,  # 只需要当前K线
        }
        
        max_lookback = max(lookback_dict.get('alpha030_year', 240), max([v for k, v in lookback_dict.items() if k != 'alpha030_year']))
        
        # 判断是批量初始化还是增量更新
        min_series_len = min(len(s) for s in series_dict.values())
        
        if min_series_len < current_len - max_lookback:
            # 批量初始化：一次性计算所有历史数据
            start_idx = max(max_lookback, min_series_len)
            for i in range(start_idx, current_len):
                self._compute_single_bar_alphas(symbol, i, close_prices, open_prices, high_prices, low_prices, volume_prices, lookback_dict, series_dict, max_length)
        else:
            # 增量更新：只计算最新一根 K 线
            i = current_len - 1
            self._compute_single_bar_alphas(symbol, i, close_prices, open_prices, high_prices, low_prices, volume_prices, lookback_dict, series_dict, max_length)
    
    def _compute_single_bar_alphas(self, symbol, i, close_prices, open_prices, high_prices, low_prices, volume_prices, lookback_dict, series_dict, max_length):
        """计算单根K线的所有因子值并添加到序列中"""
        # 调用各个独立的因子计算方法
        self._compute_alpha001(i, close_prices, lookback_dict, series_dict, max_length)
        self._compute_alpha002(i, close_prices, volume_prices, lookback_dict, series_dict, max_length)
        self._compute_alpha003(i, open_prices, close_prices, volume_prices, lookback_dict, series_dict, max_length)
        self._compute_alpha006(i, open_prices, volume_prices, lookback_dict, series_dict, max_length)
        self._compute_alpha011(i, high_prices, low_prices, close_prices, volume_prices, lookback_dict, series_dict, max_length)
        self._compute_alpha020(i, open_prices, close_prices, lookback_dict, series_dict, max_length)
        self._compute_alpha023(i, close_prices, lookback_dict, series_dict, max_length)
        self._compute_alpha027(i, close_prices, volume_prices, lookback_dict, series_dict, max_length)
        self._compute_alpha030(i, close_prices, lookback_dict, series_dict, max_length)
        self._compute_alpha032(i, high_prices, low_prices, close_prices, lookback_dict, series_dict, max_length)
        self._compute_alpha041(i, high_prices, low_prices, lookback_dict, series_dict, max_length)
        self._compute_alpha045(i, open_prices, close_prices, volume_prices, lookback_dict, series_dict, max_length)
        self._compute_alpha053(i, open_prices, close_prices, series_dict, max_length)
        self._compute_alpha057(i, high_prices, close_prices, volume_prices, lookback_dict, series_dict, max_length)
        self._compute_alpha065(i, open_prices, close_prices, lookback_dict, series_dict, max_length)
        self._compute_alpha081(i, high_prices, volume_prices, lookback_dict, series_dict, max_length)
        self._compute_alpha084(i, high_prices, close_prices, volume_prices, lookback_dict, series_dict, max_length)
        self._compute_alpha093(i, open_prices, high_prices, lookback_dict, series_dict, max_length)
        self._compute_alpha094(i, close_prices, lookback_dict, series_dict, max_length)
        self._compute_alpha101(i, open_prices, high_prices, low_prices, close_prices, series_dict, max_length)
    
    def _calculate_zscore(self, symbol, series_key):
        """计算 Z-score（基于历史序列）"""
        series = self.all_info[symbol][series_key]
        
        if len(series) == 0:
            return None
        
        zscore_window = self.all_info[symbol]["zscore_window"]
        
        # 如果数据不足，返回 0
        if len(series) < 2:
            return 0.0
        
        # 使用最近 zscore_window 个数据计算均值和标准差
        recent_series = series[-zscore_window:] if len(series) >= zscore_window else series
        
        mean = np.mean(recent_series)
        std = np.std(recent_series)
        
        if std == 0:
            return 0.0
        
        # 当前值的 Z-score
        current_value = series[-1]
        zscore = (current_value - mean) / std
        
        # 不限制范围，让 Z-score 自由分布（或者限制在更大的范围如 [-5, 5]）
        return np.clip(zscore, -5, 5)
    
    # ==================== 独立的因子计算方法（用于增量更新）====================
    
    def _compute_alpha001(self, i, close_prices, lookback_dict, series_dict, max_length):
        """Alpha #001: 收益率/波动率比率（Sharpe-like）"""
        lookback = lookback_dict['alpha001']
        if i >= lookback:
            returns = np.diff(close_prices[i-lookback:i+1]) / close_prices[i-lookback:i]
            if len(returns) > 0:
                mean_return = np.mean(returns)
                std_return = np.std(returns)
                if std_return > 0:
                    series_dict['alpha001'].append(mean_return / std_return)
                    if len(series_dict['alpha001']) > max_length:
                        series_dict['alpha001'].pop(0)
    
    def _compute_alpha002(self, i, close_prices, volume_prices, lookback_dict, series_dict, max_length):
        """Alpha #002: 价格与成交量负相关（缩量上涨/放量下跌）"""
        lookback = lookback_dict['alpha002']
        if i >= lookback:
            price_change = np.diff(close_prices[i-lookback:i+1])
            volume_change = np.diff(volume_prices[i-lookback:i+1])
            if len(price_change) >= 2 and np.std(price_change) > 0 and np.std(volume_change) > 0:
                corr = np.corrcoef(price_change, volume_change)[0, 1]
                series_dict['alpha002'].append(-corr)
                if len(series_dict['alpha002']) > max_length:
                    series_dict['alpha002'].pop(0)
    
    def _compute_alpha003(self, i, open_prices, close_prices, volume_prices, lookback_dict, series_dict, max_length):
        """Alpha #003: 跳空与成交量负相关"""
        lookback = lookback_dict['alpha003']
        if i >= lookback:
            gap = open_prices[i-lookback+1:i+1] - close_prices[i-lookback:i]
            volumes = volume_prices[i-lookback+1:i+1]
            if len(gap) >= 2 and np.std(gap) > 0 and np.std(volumes) > 0:
                corr = np.corrcoef(gap, volumes)[0, 1]
                series_dict['alpha003'].append(-corr)
                if len(series_dict['alpha003']) > max_length:
                    series_dict['alpha003'].pop(0)
    
    def _compute_alpha006(self, i, open_prices, volume_prices, lookback_dict, series_dict, max_length):
        """Alpha #006: 开盘价与成交量负相关"""
        lookback = lookback_dict['alpha006']
        if i >= lookback:
            open_change = np.diff(open_prices[i-lookback:i+1])
            volume_change = np.diff(volume_prices[i-lookback:i+1])
            if len(open_change) >= 2 and np.std(open_change) > 0 and np.std(volume_change) > 0:
                corr = np.corrcoef(open_change, volume_change)[0, 1]
                series_dict['alpha006'].append(-corr)
                if len(series_dict['alpha006']) > max_length:
                    series_dict['alpha006'].pop(0)

    def _compute_alpha011(self, i, high_prices, low_prices, close_prices, volume_prices, lookback_dict, series_dict, max_length):
        """Alpha #011: 相对VWAP偏离度"""
        lookback = lookback_dict['alpha011']
        if i >= lookback:
            typical_price = (high_prices[i-lookback+1:i+1] + low_prices[i-lookback+1:i+1] + close_prices[i-lookback+1:i+1]) / 3
            volumes = volume_prices[i-lookback+1:i+1]
            if np.sum(volumes) > 0:
                vwap = np.sum(typical_price * volumes) / np.sum(volumes)
                series_dict['alpha011'].append((close_prices[i] - vwap) / vwap)
                if len(series_dict['alpha011']) > max_length:
                    series_dict['alpha011'].pop(0)
    
    def _compute_alpha020(self, i, open_prices, close_prices, lookback_dict, series_dict, max_length):
        """Alpha #020: 开盘趋势 + 日内涨幅"""
        lookback = lookback_dict['alpha020']
        if i >= lookback:
            open_window = open_prices[i-lookback+1:i+1]
            x = np.arange(len(open_window))
            if np.std(open_window) > 0:
                slope = np.polyfit(x, open_window, 1)[0]
                intraday_return = (close_prices[i] - open_prices[i]) / open_prices[i]
                open_trend = slope / np.mean(open_window)
                series_dict['alpha020'].append(open_trend * 10 + intraday_return * 5)
                if len(series_dict['alpha020']) > max_length:
                    series_dict['alpha020'].pop(0)
    
    def _compute_alpha023(self, i, close_prices, lookback_dict, series_dict, max_length):
        """Alpha #023: 突破均线"""
        lookback = lookback_dict['alpha023']
        if i >= lookback:
            ma = np.mean(close_prices[i-lookback:i])
            breakout = (close_prices[i] - ma) / ma
            series_dict['alpha023'].append(breakout)
            if len(series_dict['alpha023']) > max_length:
                series_dict['alpha023'].pop(0)
    
    def _compute_alpha027(self, i, close_prices, volume_prices, lookback_dict, series_dict, max_length):
        """Alpha #027: 量价齐升"""
        lookback = lookback_dict['alpha027']
        if i >= lookback:
            price_return = (close_prices[i] - close_prices[i-1]) / close_prices[i-1]
            avg_volume = np.mean(volume_prices[i-lookback:i])
            if avg_volume > 0:
                volume_ratio = (volume_prices[i] - avg_volume) / avg_volume
                series_dict['alpha027'].append(price_return * 5 + volume_ratio * 2)
                if len(series_dict['alpha027']) > max_length:
                    series_dict['alpha027'].pop(0)
    
    def _compute_alpha030(self, i, close_prices, lookback_dict, series_dict, max_length):
        """Alpha #030: 月度+年度双重收益"""
        month_period = lookback_dict['alpha030_month']
        year_period = lookback_dict['alpha030_year']
        if i >= year_period:
            month_return = (close_prices[i] - close_prices[i-month_period]) / close_prices[i-month_period]
            year_return = (close_prices[i] - close_prices[i-year_period]) / close_prices[i-year_period]
            series_dict['alpha030'].append((month_return + year_return * 0.5) / 1.5)
            if len(series_dict['alpha030']) > max_length:
                series_dict['alpha030'].pop(0)

    def _compute_alpha032(self, i, high_prices, low_prices, close_prices, lookback_dict, series_dict, max_length):
        """Alpha #032: VWAP趋势"""
        lookback = lookback_dict['alpha032']
        if i >= lookback:
            vwaps = []
            for j in range(i-lookback+1, i+1):
                typical = (high_prices[j] + low_prices[j] + close_prices[j]) / 3
                vwaps.append(typical)
            vwaps = np.array(vwaps)
            if np.std(vwaps) > 0:
                x = np.arange(len(vwaps))
                slope = np.polyfit(x, vwaps, 1)[0]
                trend = slope / np.mean(vwaps)
                series_dict['alpha032'].append(trend)
                if len(series_dict['alpha032']) > max_length:
                    series_dict['alpha032'].pop(0)
    
    def _compute_alpha041(self, i, high_prices, low_prices, lookback_dict, series_dict, max_length):
        """Alpha #041: 中间价创新高"""
        lookback = lookback_dict['alpha041']
        if i >= lookback:
            mid_prices = (high_prices[i-lookback:i+1] + low_prices[i-lookback:i+1]) / 2
            current_mid = mid_prices[-1]
            max_mid = np.max(mid_prices[:-1])
            if max_mid > 0:
                breakout = (current_mid - max_mid) / max_mid
                series_dict['alpha041'].append(breakout)
                if len(series_dict['alpha041']) > max_length:
                    series_dict['alpha041'].pop(0)
    
    def _compute_alpha045(self, i, open_prices, close_prices, volume_prices, lookback_dict, series_dict, max_length):
        """Alpha #045: 开盘/收盘/成交量综合"""
        lookback = lookback_dict['alpha045']
        if i >= lookback:
            open_change = (open_prices[i] - open_prices[i-lookback]) / open_prices[i-lookback]
            close_change = (close_prices[i] - close_prices[i-lookback]) / close_prices[i-lookback]
            avg_volume = np.mean(volume_prices[i-lookback:i])
            volume_change = (volume_prices[i] - avg_volume) / avg_volume if avg_volume > 0 else 0
            series_dict['alpha045'].append((open_change + close_change) * 2 + volume_change)
            if len(series_dict['alpha045']) > max_length:
                series_dict['alpha045'].pop(0)
    
    def _compute_alpha053(self, i, open_prices, close_prices, series_dict, max_length):
        """Alpha #053: 跳空高开且强势收盘"""
        if i >= 1:
            gap_up = (open_prices[i] - close_prices[i-1]) / close_prices[i-1]
            intraday_strength = (close_prices[i] - open_prices[i]) / open_prices[i]
            if gap_up > 0 and intraday_strength >= 0:
                series_dict['alpha053'].append(gap_up * 5 + intraday_strength * 3)
            else:
                series_dict['alpha053'].append(-0.1)
            if len(series_dict['alpha053']) > max_length:
                series_dict['alpha053'].pop(0)
    
    def _compute_alpha057(self, i, high_prices, close_prices, volume_prices, lookback_dict, series_dict, max_length):
        """Alpha #057: 接近高点+放量"""
        lookback = lookback_dict['alpha057']
        if i >= lookback:
            recent_high = np.max(high_prices[i-lookback:i])
            price_position = (close_prices[i] - recent_high) / recent_high if recent_high > 0 else 0
            avg_volume = np.mean(volume_prices[i-lookback:i])
            volume_ratio = (volume_prices[i] - avg_volume) / avg_volume if avg_volume > 0 else 0
            series_dict['alpha057'].append(price_position * 10 + volume_ratio * 2)
            if len(series_dict['alpha057']) > max_length:
                series_dict['alpha057'].pop(0)

    def _compute_alpha065(self, i, open_prices, close_prices, lookback_dict, series_dict, max_length):
        """Alpha #065: 开盘价与收盘价同步性"""
        lookback = lookback_dict['alpha065']
        if i >= lookback:
            close_changes = np.diff(close_prices[i-lookback:i+1])
            open_changes = np.diff(open_prices[i-lookback:i+1])
            if len(close_changes) >= 2 and np.std(close_changes) > 0 and np.std(open_changes) > 0:
                corr = np.corrcoef(close_changes, open_changes)[0, 1]
                series_dict['alpha065'].append(corr)
                if len(series_dict['alpha065']) > max_length:
                    series_dict['alpha065'].pop(0)
    
    def _compute_alpha081(self, i, high_prices, volume_prices, lookback_dict, series_dict, max_length):
        """Alpha #081: 创新高+放量"""
        lookback = lookback_dict['alpha081']
        if i >= lookback:
            recent_high = np.max(high_prices[i-lookback:i])
            new_high = (high_prices[i] - recent_high) / recent_high if recent_high > 0 else 0
            avg_volume = np.mean(volume_prices[i-lookback:i])
            volume_surge = (volume_prices[i] - avg_volume) / avg_volume if avg_volume > 0 else 0
            if new_high > 0 and volume_surge > 0:
                series_dict['alpha081'].append(new_high * 10 + volume_surge * 3)
            else:
                series_dict['alpha081'].append(-0.1)
            if len(series_dict['alpha081']) > max_length:
                series_dict['alpha081'].pop(0)
    
    def _compute_alpha084(self, i, high_prices, close_prices, volume_prices, lookback_dict, series_dict, max_length):
        """Alpha #084: 接近高点+放量"""
        lookback = lookback_dict['alpha084']
        if i >= lookback:
            recent_high = np.max(high_prices[i-lookback:i])
            price_ratio = close_prices[i] / recent_high if recent_high > 0 else 1
            avg_volume = np.mean(volume_prices[i-lookback:i])
            volume_ratio = volume_prices[i] / avg_volume if avg_volume > 0 else 1
            series_dict['alpha084'].append((price_ratio - 0.95) * 20 + (volume_ratio - 1) * 2)
            if len(series_dict['alpha084']) > max_length:
                series_dict['alpha084'].pop(0)
    
    def _compute_alpha093(self, i, open_prices, high_prices, lookback_dict, series_dict, max_length):
        """Alpha #093: 开盘创新高"""
        lookback = lookback_dict['alpha093']
        if i >= lookback:
            recent_high = np.max(high_prices[i-lookback:i])
            gap_strength = (open_prices[i] - recent_high) / recent_high if recent_high > 0 else 0
            if gap_strength > 0:
                series_dict['alpha093'].append(gap_strength)
            else:
                series_dict['alpha093'].append(-0.1)
            if len(series_dict['alpha093']) > max_length:
                series_dict['alpha093'].pop(0)
    
    def _compute_alpha094(self, i, close_prices, lookback_dict, series_dict, max_length):
        """Alpha #094: 连续上涨天数"""
        lookback = lookback_dict['alpha094']
        if i >= lookback:
            consecutive_up = 0
            for j in range(1, lookback + 1):
                if close_prices[i-j+1] > close_prices[i-j]:
                    consecutive_up += 1
                else:
                    break
            if consecutive_up > 0:
                total_return = (close_prices[i] - close_prices[i-consecutive_up]) / close_prices[i-consecutive_up]
                strength = consecutive_up / lookback + total_return * 2
                series_dict['alpha094'].append(strength)
            else:
                series_dict['alpha094'].append(-0.2)
            if len(series_dict['alpha094']) > max_length:
                series_dict['alpha094'].pop(0)
    
    def _compute_alpha101(self, i, open_prices, high_prices, low_prices, close_prices, series_dict, max_length):
        """Alpha #101: 强势收盘位置"""
        if i >= 0:
            high = high_prices[i]
            low = low_prices[i]
            close = close_prices[i]
            open_price = open_prices[i]
            if high > low:
                close_position = (close - low) / (high - low)
                intraday_return = (close - open_price) / open_price if open_price > 0 else 0
                series_dict['alpha101'].append((close_position - 0.5) * 2 + intraday_return * 3)
            else:
                series_dict['alpha101'].append(0.0)
            if len(series_dict['alpha101']) > max_length:
                series_dict['alpha101'].pop(0)
